{% extends "enter_results.html" %}
{% load static debate_tags i18n %}

{% block page-subnav-sections %}
  <a class="btn btn-outline-primary" href="{% tournamenturl 'results-assistant-round-list' %}">
    <i data-feather="chevron-left"></i>
    {% trans "Back to All Results" %}
  </a>
  {% if not new %}
    <a class="btn btn-primary" href="{% tournamenturl 'results-assistant-ballotset-new' debate.id %}">
      {% trans "Create a new ballot set for this debate" %}
    </a>
  {% endif %}
{% endblock %}

{% block content %}
  <form id="resultsForm" action="." method="POST">
    {% csrf_token %}
    {% include "ballot/ballot_set.html" %}

    <div class="card mt-3">
      <div class="card-body">
        {% if new and not pref.disable_ballot_confirms %}
          {# After a new ballot, always go into DRAFT, and the new ballot is neither discarded nor confirmed. #}
          <input id="id_debate_result_status" type="hidden" name="debate_result_status" value="{{ debate.STATUS_DRAFT }}" />
          <input id="id_discarded" type="hidden" name="discarded" value="False" />
          <input id="id_confirmed" type="hidden" name="confirmed" value="False" />
          <input class="btn btn-block btn-success" type="submit" value="Save draft results" tabindex="{{ form.nexttabindex }}" />
        {% else %}
          {# If confirming, always go into CONFIRMED. If incorrect, go into DRAFT or NONE depending on whether there are still ballots left. #}
          {% if not new and request.user == ballotsub.submitter %}
            <div class="alert alert-danger">
              {% trans "You can't confirm this ballot set because you entered it." %}
            </div>
          {% endif %}
          <input id="id_debate_result_status" type="hidden" name="debate_result_status" value="{{ debate.STATUS_CONFIRMED }}" />
          <input id="id_discarded" type="hidden" name="discarded" value="False" />
          <input id="id_confirmed" type="hidden" name="confirmed" value="True" />
          <div class="row">
            <div class="col">
              <input id="correct" {% if request.user == ballotsub.submitter %}disabled="disabled"{% endif %} class="btn {% if request.user != ballotsub.submitter %}btn-success{% endif %} btn-block" type="submit" value="{% trans "Confirm results" %}" tabindex="{{ form.nexttabindex }}" />
            </div>
            <div class="col">
              <input id="incorrect" class="btn btn-danger btn-block" type="button" value="{% trans "Results are incorrect" %}" tabindex="{{ form.nexttabindex|add:1 }}" />
            </div>
          </div>
          <div class="text-center pt-3 small text-muted">
            {% if pref.enable_ballot_receipts and not request.user == ballotsub.submitter %}
              {% trans "Emails will be sent to adjudicators when the ballot is confirmed." %}
            {% endif %}
          </div>
        {% endif %}
      </div>
    </div>

  </form>
{% endblock content %}

{% block js %}
{{ block.super }}
<script type="text/javascript">
  $(document).ready( function() {
    {% if not new %}
      function preSubmit() {
        $('#resultsForm input').removeAttr('readonly');
        $('#resultsForm select').removeAttr('disabled');
        $('#resultsForm input[type="checkbox"]').removeAttr('disabled');
      }
      $('#resultsForm input[type="number"]').attr('readonly', 'true');
      $('#resultsForm input[type="checkbox"]').attr('disabled', 'true');
      $('#resultsForm select').attr('disabled', 'true');
      $('#incorrect').click(function() {
        {% if all_ballotsubs.count > 1 %}
          $('#id_debate_result_status').val('{{ debate.STATUS_DRAFT }}');
        {% else %}
          $('#id_debate_result_status').val('{{ debate.STATUS_NONE }}');
        {% endif %}
        $('#id_discarded').attr('value', 'True');
        $('#id_confirmed').attr('value', 'False');
        preSubmit();
        $('#resultsForm').submit();
        return false;
      });
      $('#correct').click(function() {
        preSubmit();
      });
    {% endif %}
  });
</script>
{% endblock %}
